package ext.opensource.netty.server.mqtt.api.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import ext.opensource.netty.common.core.CacheList;
import ext.opensource.netty.common.core.CacheListLocalMemory;
import ext.opensource.netty.server.mqtt.api.ConsumerDataService;
import ext.opensource.netty.server.mqtt.common.ConsumerMessage;
import ext.opensource.netty.server.mqtt.protocol.data.ConsumerClientData;

public class ConsumerDataServiceImpl implements ConsumerDataService {
	private CacheList<ConsumerClientData> sureDataCache;

	public ConsumerDataServiceImpl() {
		sureDataCache = new CacheListLocalMemory<ConsumerClientData>();
	}

	@Override
	public void setConsumerCacheList(final CacheList<ConsumerClientData> cache) {
		if (cache != null) {
			this.sureDataCache = cache;
		}
	}

	@Override
	public void putPublishMessage(final String clientId, final ConsumerMessage dupPublishMessage) {
		ConsumerClientData map = sureDataCache.containsKey(clientId) ? sureDataCache.get(clientId) : null;
		if (map == null) {
			map = new ConsumerClientData(clientId);
		}
		map.put(dupPublishMessage.getMessageId(), dupPublishMessage);
		sureDataCache.put(clientId, map);
	}

	@Override
	public List<ConsumerMessage> getPublishMessage(final String clientId) {
		if (sureDataCache.containsKey(clientId)) {
			final ConsumerClientData map = sureDataCache.get(clientId);
			final Collection<ConsumerMessage> collection = map.values();
			return new ArrayList<ConsumerMessage>(collection);
		}
		return new ArrayList<ConsumerMessage>();
	}

	@Override
	public void removePublishMessage(final String clientId, final int messageId) {
		if (sureDataCache.containsKey(clientId)) {
			final ConsumerClientData map = sureDataCache.get(clientId);
			if (map.containsKey(messageId)) {
				map.remove(messageId);
				if (map.size() > 0) {
					sureDataCache.put(clientId, map);
				} else {
					sureDataCache.remove(clientId);
				}
			}
		}
	}

	@Override
	public void removeByClient(final String clientId) {
		if (sureDataCache.containsKey(clientId)) {
			sureDataCache.remove(clientId);
		}
	}
}
